<?php

namespace api\modules\mini\forms\spoker;

use common\helpers\ValidatorHelper;
use common\models\spoker\Spoker;
use common\models\user\User;
use Yii;
use yii\base\Model;

/**
 * Class SpokerForm
 * @package api\modules\mini\forms\spoker
 */
class SpokerForm extends Model
{
    /**
     * @var string $name 姓名
     * @var string $mobile 手机号
     * @var string $referrer_name 推荐人
     */
    public $name;
    public $mobile;
    public $referrer_name;

    /**
     * @var User $_user
     */
    protected $_user;


    public function rules()
    {
        return [
            // 参数验证
            [['name', 'mobile'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],

            [['name'], 'checkName', 'skipOnEmpty' => false, 'on' => ['create']],
            [['mobile'], 'checkMobile', 'skipOnEmpty' => false, 'on' => ['create']],
            [['referrer_name'], 'checkReferrerName', 'skipOnEmpty' => false, 'on' => ['create']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['create'] = ['name', 'mobile', 'referrer_name'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'name' => '姓名',
            'mobile' => '手机号',
            'referrer_name' => '推荐人',
        ];
    }

    /**
     * 检验姓名
     * @param $attribute
     * @return void
     */
    public function checkName($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }

        if (mb_strlen($this->name) > 20) {
            $this->addError($attribute, '姓名过长[1-20位]');
            return;
        }
    }

    /**
     * 检验手机号
     * @param $attribute
     * @return void
     */
    public function checkMobile($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }

        $user = $this->getUser();
        if ($user->spoker_id) {
            $this->addError($attribute, '存在代言人，请勿重复申请');
            return;
        }
        if (ValidatorHelper::validateMobile($this->mobile) === false) {
            $this->addError($attribute, '请输入正确手机号');
            return;
        }
        /*$exist = Spoker::findOne(['user_id' => Yii::$app->user->identity->member_id, 'mobile' => $this->mobile, 'status' => [Spoker::STATUS_WAIT, Spoker::STATUS_SUCCESS]]);
        if ($exist) {
            $this->addError($attribute, '手机号重复');
            return;
        }*/
    }

    /**
     * 检验推荐人
     * @param $attribute
     * @return void
     */
    public function checkReferrerName($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }

        if (mb_strlen($this->referrer_name) > 20) {
            $this->addError($attribute, '推荐人姓名过长[1-20位]');
            return;
        }
    }

    public function getUser(): User
    {
        if (is_null($this->_user)) {
            $this->_user =  Yii::$app->user->identity->user;
        }
        return $this->_user;
    }
}